package com.ai.module.chat.config.advice;

import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.common.BaseResp;
import com.ai.module.mj.constant.stringConstant.ReturnCode;
import com.alibaba.fastjson2.JSONObject;
import com.jayway.jsonpath.PathNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;

/**
 * @author: MrHe
 * @create: 2023-11-23 21:39
 * @description:
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler<T> {

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public BaseResp<String> handleMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        exception.printStackTrace();
        log.error("通用异常打印日志,message[{}], exception [{}],code[{}]",
                exception.getMessage()
                ,exception
                ,HttpStatus.METHOD_NOT_ALLOWED.value());
        // 处理不支持的HTTP方法请求异常
        return BaseResp.fail("Method Not Allowed: " + exception.getMessage(),HttpStatus.METHOD_NOT_ALLOWED.value());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public BaseResp<String> handleValidationExceptions(MethodArgumentNotValidException exception) {
        // 处理参数校验异常
        String errorMessage = exception.getBindingResult().getFieldErrors().stream()
                .map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
                .collect(Collectors.joining(", "));
        exception.printStackTrace();
        log.error("通用异常打印日志,message[{}], exception[{}],code[{}]",
                exception.getMessage()
                , exception
                , HttpStatus.BAD_REQUEST.value());
        return BaseResp.fail("Validation Error: " + errorMessage,HttpStatus.BAD_REQUEST);
    }




    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResp<String> dealException(Exception exception) {
        exception.printStackTrace();
        log.error("通用异常打印日志,message[{}], exception [{}]",
                exception.getMessage()
                , exception );
        // 处理通用异常
        return BaseResp.fail("系统繁忙,请稍后再试", ReturnCode.FAILURE);
    }


    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResp<String> dealException(BusinessException exception) {
        exception.printStackTrace();
        int code = exception.getCode();
        if (code != 0) {
            return BaseResp.fail(exception.getMessage(),code);
        }
        StackTraceElement[] stackTrace = exception.getStackTrace();

        log.error("通用异常打印日志,message[{}], msg [{}],exception[{}]",
                exception.getMessage()
                , JSONObject.toJSONString(exception.getMsg())
                , exception
        );
        // 处理通用异常
        return BaseResp.fail(exception.getMessage(),code);
    }


    @ExceptionHandler(Throwable.class)
    public BaseResp<String> dealException(Throwable exception) {
        exception.printStackTrace();
        StackTraceElement[] stackTrace = exception.getStackTrace();
        log.error("通用异常打印日志,message[{}], msg [{}],exception[{}]",
                exception.getMessage()
                , JSONObject.toJSONString(exception.getLocalizedMessage())
                , exception
        );
        // 处理通用异常
        return BaseResp.fail( exception.getMessage());
    }


    @ExceptionHandler(PathNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public BaseResp<String> dealException(PathNotFoundException exception) {
        exception.printStackTrace();

        // 处理通用异常
        return BaseResp.fail( exception.getMessage(),HttpStatus.NOT_FOUND);
    }

}
